from flask import Flask,render_template,request,escape

app = Flask(__name__)

def log_request(req:"flask_request",res:str):
	with open("view.log","a") as log:
		print(req.form,req.remote_addr,req.user_agent,res,file = log,sep= "|")

@app.route("/")
def Hello():
	return "Hello"

@app.route("/entry")
def hello_entry():
	return render_template("entry.html",the_title = "hello_entry web")

@app.route("/search",methods = ["POST"])
def Hello_search():
	phrase = request.form["phrase"]
	letters = request.form["letters"]
	results = phrase + letters
	log_request(request,results)
	return render_template("results.html",the_results = "results")

@app.route("/viewlog")
def hello_viewlog():
	with open("view.log","r") as log:
		contents = []
		for item in log:
			contents.append([])
			for i in item.split("|"):
				contents[-1].append(escape(i))
	# return str(contents)
		
	log_title = ["Form data","Remote_addr","User_agent","results"]
	return render_template("view.html",the_data = contents,the_log_title = log_title)

if __name__ == "__main__":
	app.run(debug=True)